package com.yungu.swift.api.route.v1.driver.h5;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.DriverCheckDto;
import com.yungu.swift.assets.driver.service.DriverCheckService;
import com.yungu.swift.constants.DriverConstant;
import com.yungu.swift.utils.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import static com.yungu.swift.api.config.NacosListener.SYS_CONFIG;

@Controller
@RequestMapping(value = "/h5/driver")
public class DriverH5Controller {

    @Reference
    DriverCheckService driverCheckService;

    @RequestMapping(value = {"/register"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String index(@RequestParam String appid,
                        @RequestParam String agentUuid,
                        @RequestParam String companyUuid,
                        @RequestParam Integer type,
                        Model model) {
        model.addAttribute("type", type);
        model.addAttribute("appid", appid);
        model.addAttribute("agentUuid", agentUuid);
        model.addAttribute("companyUuid", companyUuid);
        return "commonPage/index";
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String add(@RequestParam String appid,
                      @RequestParam String agentUuid,
                      @RequestParam String companyUuid,
                      @RequestParam String mobile,
                      @RequestParam Integer type, Model model) {
        model.addAttribute("mobile", mobile);
        model.addAttribute("appid", appid);
        model.addAttribute("agentUuid", agentUuid);
        model.addAttribute("companyUuid", companyUuid);
        model.addAttribute("type", type);
        Map<String, Object> map = new HashMap<>(8);
        map.put("mobile", mobile);
        map.put("appid", appid);
        map.put("type", type);
        DriverCheckDto driverCheckDto = driverCheckService.getLatest(map).getData();
        model.addAttribute("driverCheckDto", driverCheckDto);
        if(driverCheckDto!=null&&driverCheckDto.getGetDriverLicenseDate()!=null){
            model.addAttribute("getDriverLicenseDate",driverCheckDto.getGetDriverLicenseDate().getTime());
        }
        model.addAttribute("website", SYS_CONFIG.getWebSite());
        return "commonPage/add";
    }

    @RequestMapping(value = {"/check"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String check(@RequestParam String appid, @RequestParam String mobile, @RequestParam Integer type, Model model) {
        Map<String, Object> map = MapUtils.build();
        map.put("mobile", mobile);
        map.put("appid", appid);
        map.put("type", type);
        DriverCheckDto driverCheckDto = driverCheckService.get(map).getData();
        model.addAttribute("model", driverCheckDto);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        driverCheckDto.setDrivingRegisteredTimeStr(sdf.format(driverCheckDto.getCreateOn()));
        String checkMsg = "（预计需要七个工作日）";
        String msg = "认证中";
        //审核通过
        if (driverCheckDto.getStatus().intValue() == DriverConstant.DRIVER_CHECK_PASS.intValue()) {
            checkMsg = "(您已经是平台司机了)";
            msg = "认证通过";
            //审核驳回
        } else if (driverCheckDto.getStatus().intValue() == DriverConstant.DRIVER_CHECK_NOT_PASS) {
            msg = "认证失败";
            checkMsg = "原因：" + driverCheckDto.getRemark();
        } else {//审核中
        }
        model.addAttribute("checkMsg", checkMsg);
        model.addAttribute("msg", msg);
        model.addAttribute("bean", driverCheckDto);
        if (appid.equals(SYS_CONFIG.getAppid())) {
            return "commonPage/check";
        }
        return null;
    }

    @RequestMapping(value = {"/userAgreement"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String index(@RequestParam String appid) {
        return appid + "/driverAgreement";
    }

    @RequestMapping(value = {"/goodsAgreement"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String goodsAgreement(@RequestParam String appid) {
        return appid + "/goodsAgreement";
    }
}